/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.actions;

import com.myapp.struts.forms.TriangleCalculatorForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/*
 * TODO
 *      Need to put a good implementation of error message setting. 
 * /

/**
 *
 * @author sivakumar pabolu
 */
public class TriangleCalculatorAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "successFwd";
    private static final String DEFAULT= "defaultFwd";
    private static final String HOME ="homeFwd";

    private static final String submitUsrActn="submit";
    private static final String homeUsrActn="home";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TriangleCalculatorForm myForm = (TriangleCalculatorForm)form;

        String fwd = DEFAULT; 
        String userAction = myForm.getUserAction();

        if( userAction!=null && !userAction.isEmpty() ){
            if(userAction.equalsIgnoreCase(submitUsrActn)){
                fwd=handleSubmit(request, myForm); 
            } else if (userAction.equalsIgnoreCase(homeUsrActn)){
                fwd=handleHome(request, myForm);
            }
        } else  {
            addErrorMessage(request, "userAction", "Something is wrong, i did not understand " +
                    "what you want me to do...  :( ");
        }
             
        return mapping.findForward(fwd);
    }

    /**
     * Method to do the stuff that needs to happen if the user has clicked
     * submit button. 
     * It will return a String which corresponds to the forward that the user
     * needs to taken to. 
     * 
     * @param request
     * @param form
     * @return
     */
    private String handleSubmit(HttpServletRequest request, TriangleCalculatorForm myForm)
    {
       String fwdReturned = SUCCESS;
       try {
            System.out.println("sideA = " + myForm.getSideA());
            System.out.println("resultA = " + myForm.getAngleA());

            

            myForm.setResult("Antaa raama mayam.... !!!!!!");
       } catch (Exception ex){
           System.out.println("Encountered exception : "+ ex.getMessage());
           addErrorMessage(request,"userAction", ex.getMessage());
           fwdReturned = DEFAULT;
       }
       return fwdReturned;
    }

    /**
     * Method to do the stuff that needs to happen if the user has clicked
     * submit button.
     * It will return a String which corresponds to the forward that the user
     * needs to taken to.
     *
     * @param request
     * @param form
     * @return
     */
    private String handleHome(HttpServletRequest request, TriangleCalculatorForm form)
    {
       String fwdReturned = HOME;
       try {
            System.out.println("need to do any session reset if needed. ");
       } catch (Exception ex){
           System.out.println("Encountered exception : "+ ex.getMessage());
           addErrorMessage(request, "userAction", ex.getMessage());
           fwdReturned = DEFAULT;
       }
       return fwdReturned;
    }

    private void addErrorMessage(HttpServletRequest request, String property, String errMessage){
        System.out.println("addErrorMessage: Adding errMessage: " + errMessage);
        ActionErrors errors = new ActionErrors();
        errors.add(property, new ActionMessage(errMessage, false));
        this.addErrors(request, errors);
    }
    
}
